function resolvePromise(promise2,x,resolve,reject) {
  if (promise2 == x) {
    return reject(new TypeError('自己等待自己完成是错误的实现，用一个类型错误，结束掉 promise '))
  }
  let flag = false
  if ((typeof x === 'object' && x !== null) || typeof x == 'function') {
    try {
      let then = x.then
      if (typeof then == 'function') {
        then.call(x,y => {
          if(flag) return
          resolvePromise(promise2, y, resolve, reject); 
          flag = true
        },r => {
          if (flag) return
          reject(r)
          flag = true
        })
      } else {
        resolve(x)
      }
    } catch(err) {
      if(flag) return
      reject(new TypeError('如果 x.then 抛出了异常，就直接 reject'))
      flag = true
    }
  } else {
    resolve(x)
  }
}

const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class MyPromise {
    constructor(executor) {
        this.status = PENDING
        this.value = null
        this.reason = null

        this.onResolveCallbacks = []
        this.onRejectCallbacks = []
        let resolve = (value) => {
            // 此处是因为promise的状态只能从pending-->fulfilled或者pending-->rejectd
            if (this.status == PENDING) {
                this.status = FULFILLED
                this.value = value

                this.onResolveCallbacks.forEach(fn => fn())
            }
        }
        let reject = reason => {
            if (this.status == PENDING) {
                this.status = REJECTED
                this.reason = reason
                this.onRejectCallbacks.forEach(fn => fn())
            }
        }

        // 立即执行器
        try {
            executor(resolve,reject)
        } catch(err) {
            reject(err)
        }
    }
    then(onFulfilled,onRejected) {
        onFulfilled = typeof onFulfilled == 'function' ? onFulfilled : v => v
        onRejected = typeof onRejected == 'function' ? onRejected :  err => { throw err }
        let promise2 = new MyPromise((resolve,reject) => {
            if (this.status == FULFILLED) {
                setTimeout(() => {
                    let x = onFulfilled(this.value)
                    resolvePromise(promise2,x,resolve,reject)
                },0)
            }
            if (this.status == REJECTED) {
                setTimeout(() => {
                    let x = onRejected(this.reason)
                    resolvePromise(promise2,x,resolve,reject)
                },0)
            }
            if (this.status == PENDING) {
                this.onResolveCallbacks.push(() => {
                    setTimeout(() => {
                        let x =  onFulfilled(this.value)
                        resolvePromise(promise2,x,resolve,reject)
                    },0)
                })
                this.onRejectCallbacks.push(() => {
                    setTimeout(() => {
                        let x = onRejected(this.reason)
                        resolvePromise(promise2,x,resolve,reject)
                    },0)
                })
            }
        })
        return promise2
    }
}

const myPromise = new MyPromise((resolve,reject )=> {
    setTimeout(() => {
        resolve('success')
        /**
         * 此行代码不生效，因为promise的状态一旦改变就不会在变，
         * 上边已经将promise的状态由pending-->fulfilled,
         * 所以下边改变状态的代码不会在实现
         */
        reject('err')
    })
}).then().then().then().then(res => {
    console.log('链式调用',res)
})